<template>
  <div class="pagination">
<!--    <h3>当前页：{{ this.pageNo }}，总页：{{ this.totalPage }}，连续页长度：{{ this.continues }}</h3>-->
<!--    <h3>start页：{{ startNumAndendNum.start }}，end页：{{ startNumAndendNum.end }}</h3>-->
    <!-- 上-->
    <button :disabled="pageNo==1" @click="goPageNo(pageNo-1)">上一页</button>
    <!--    第一页-->
    <button v-if="pageNo>Math.ceil(continues/2)" @click="goPageNo(1)" :class="{active:pageNo==1}">1</button>
    <button v-if="pageNo>1+Math.ceil(continues/2)">···</button>
    <!-- 中-->
    <button v-for="(n,index) in startNumAndendNum.end-startNumAndendNum.start+1"
            :key="index" @click="goPageNo(startNumAndendNum.start+n-1)"
            :class="{active:pageNo==startNumAndendNum.start+n-1 }">{{ startNumAndendNum.start + n - 1 }}
    </button>
    <!-- 下-->
    <button v-if="pageNo<totalPage-Math.ceil(continues/2)">···</button>
    <!--    最后一页-->
    <button v-if="pageNo<totalPage-Math.ceil(continues/2)+1"
            @click="goPageNo(totalPage)"
            :class="{active:pageNo==totalPage}">{{ this.totalPage }}
    </button>
    <button :disabled="pageNo==totalPage" @click="goPageNo(pageNo+1)">下一页</button>
    <!--数据列表的数据条数-->
    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
</template>

<script>

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Pagination",
  props: ['pageNo', 'pageSize', 'continues','total'],
  computed: {
    //分页器 总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    //分页器 连续页 确定开始start，末尾end的值
    startNumAndendNum() {
      let start = 0;
      let end = 0;
      const {pageNo, totalPage, continues} = this;
      if (totalPage < continues) {
        start = 1;
        end = totalPage;
      } else {
        start = pageNo - Math.floor(continues / 2);
        end = pageNo + Math.floor(continues / 2);
        if (start < 1) {
          start = 1;
          end = continues;
        }
        if (end > totalPage) {
          start = totalPage - continues + 1;
          end = totalPage;
        }
      }
      return {start, end}
    }
  },
  methods: {
    goPageNo(pageNo) {
      this.$emit('goPageNo', pageNo)
    }
  }
}
</script>

<style lang="less" scoped>
.pagination {
  .active {
    background-color: skyblue;
  }

  text-align: center;

  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
